package com.xxx.video.main.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.adapters.BaseViewHolder;
import com.xxx.video.commen.utils.CountUtils;
import com.xxx.video.commen.utils.DimensionUtil;
import com.xxx.video.commen.utils.DownloadUtils;
import com.xxx.video.commen.utils.ResourcesUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.main.beans.VideoListBean;

import butterknife.BindView;

public class HomeFragmentGridVideoListAdapter extends BaseRecycleViewAdapter<VideoListBean, HomeFragmentGridVideoListAdapter.MyHolder> {

    public HomeFragmentGridVideoListAdapter(@NonNull Context context) {
        super(context);
    }

    @Override
    public MyHolder onCreateDefaultViewHolder(View view, int viewType) {
        return new MyHolder(view);
    }

    @Override
    public int getItemLayout(int viewType) {
        return R.layout.item_home_fragment_grid_video_list;
    }


    class MyHolder extends BaseViewHolder {


        @BindView(R.id.iv_item_home_fragment_grid_video_list_cover)
        ImageView ivItemHomeFragmentGridVideoListCover;
        @BindView(R.id.tv_item_home_fragment_grid_video_list_play_count)
        TextView tvItemHomeFragmentGridVideoListPlayCount;
        @BindView(R.id.tv_item_home_fragment_grid_video_list_comment_count)
        TextView tvItemHomeFragmentGridVideoListCommentCount;
        @BindView(R.id.tv_item_home_fragment_grid_video_list_time)
        TextView tvItemHomeFragmentGridVideoListTime;
        @BindView(R.id.tv_item_home_fragment_grid_video_list_desc)
        TextView tvItemHomeFragmentGridVideoListDesc;
        @BindView(R.id.tv_item_home_fragment_grid_video_list_download_count)
        TextView tvItemHomeFragmentGridVideoListDownloadCount;
        @BindView(R.id.tv_item_home_fragment_grid_video_list_type)
        TextView tvItemHomeFragmentGridVideoListType;


        public MyHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData2View(int position) {
            VideoListBean listBean = getItem(position);
            /*
             * title
             */
            tvItemHomeFragmentGridVideoListDesc.setText(StringUtils.getInstance().getNotEmptyMsg(listBean.fTitle));
            /*
             * cover
             */
            Glide.with(context).load(ResourcesUtils.getInstance().getImagePath(listBean.fPicurl))
                    .apply(new RequestOptions()
                            .error(R.drawable.bg_place_holder_nine_five)
                            .placeholder(R.drawable.bg_place_holder_nine_five)
                            .transform(new RoundedCorners(DimensionUtil.dpToPx(5))))
                    .into(ivItemHomeFragmentGridVideoListCover);
            /*
             * play count
             */
            tvItemHomeFragmentGridVideoListPlayCount.setText(CountUtils.getInstance().getCount(listBean.fPlayTimes));
            /*
             *comment count
             */
            tvItemHomeFragmentGridVideoListCommentCount.setText(CountUtils.getInstance().getCount(listBean.fCommentTimes));
            /*
             * download count
             */
            tvItemHomeFragmentGridVideoListDownloadCount.setText(CountUtils.getInstance().getCount(listBean.fDownloadTimes));
            tvItemHomeFragmentGridVideoListDownloadCount.setOnClickListener(v -> DownloadUtils.getInstance().startDownload(context, listBean.fVideo, listBean.fContentId,
                    listBean.fTitle, listBean.fPicurl, listBean.fCreateTime, listBean.fPlayTimes, listBean.fCommentTimes, 0));
            /*
             * time
             */
            tvItemHomeFragmentGridVideoListTime.setText(StringUtils.getInstance().getNotEmptyMsg(String.valueOf(listBean.fCreateTime)));
            /*
             * type
             */
            /*
             * type
             */
            String type = String.format("%s﹒%s", StringUtils.getInstance().getNotEmptyMsg(String.valueOf(listBean.fTypeName)), StringUtils.getInstance().getNotEmptyMsg(String.valueOf(listBean.fLabel)));
            if (TextUtils.isEmpty(listBean.fTypeName) || TextUtils.isEmpty(listBean.fLabel)) {
                type = type.replace("﹒", "");
            }
            /*
             *label
             */
            tvItemHomeFragmentGridVideoListType.setText(type);
        }
    }
}
